### JAVA PARAMETERS - DO NOT CHANGE.
params <- list()
results <- list()
work <- list()

web <- TRUE

params$result<-"results.json"

if(web){
          params$file<-"@@@file@@@"
          params$pvalue<-as.numeric("@@@pvalue@@@")
          
          
          params$sessionid <- "@@@sessionid@@@"
          params$taskid <- "@@@taskid@@@"
          params$workspace <- "@@@workspace@@@"
}

if(!web){
          params$file<-"/Users/kennyhelsens/Desktop/vaadin_huber_test.csv"
          params$pvalue<-as.numeric("0.05")
          
          
          params$sessionid <- 223456789
          params$taskid <- 187654321
          params$workspace <- "~/tmp/vaadinR"
}

# Functions
path.sep <- function (x) {
          if (x == "windows") {
                    "\\"
          } else { 
                    "/"
          }
}


# Set workspace by session
path.sep <- path.sep(.Platform$OS.type)

params$session.workspace <- paste(params$workspace, params$sessionid, params$taskid, sep=path.sep)
if(exists(params$session.workspace) == FALSE){
          dir.create(params$session.workspace, recursive=T)
}
setwd(params$session.workspace)


# Define results targets
results<-list()

results$image$file<-paste("plot.png", sep="")
results$image$title<-"plot"

results$txt$file<-"log.txt"
results$txt$title<-"txt"

results$table$file <- "outliers.csv" 
results$table$title <- "outliers"

results$sessionid <- params$sessionid
results$dir <- getwd()


## LOAD LIBS
library(Hmisc)
library(RJSONIO)
library(MASS)
library(ggplot2)

### BEGIN SCRIPT
## BUILD DATA
x<-read.csv(params$file, header=F)

## RENAME DATA
colnames(x) <- c("id","value")

## Calculate huber stats
work$fold <- abs(qnorm(params$pvalue/1))
work$huber <- huber(x$value, k=work$fold)

work$limit.up <- work$huber$mu + work$fold*work$huber$s
work$limit.down <- work$huber$mu - work$fold*work$huber$s

## MAKE PLOT
plot.x.delta <- (work$limit.up-work$limit.down) * 0.2
png(results$image$file, height=200, width=400)
p <- ggplot(data=x, aes(x=value, y=..density..))
p <- p + geom_density()
p <- p + theme_bw()

p <- p + xlim(c(work$limit.down - plot.x.delta, work$limit.up + plot.x.delta))
p <- p + geom_vline(xintercept=c(work$limit.down, work$limit.up), linetype="dotted")
p <- p + geom_vline(xintercept=c(work$huber$mu), linetype="dotted", colour="green")
print(p)
dev.off()

## PRINT TABLE
outlier <- unlist(lapply(X=x$value, FUN=function(a){
          if(a > work$limit.up){
                    return(T)
          }else if(a < work$limit.down){
                    return(T)
          }else {
                    return(F)
          }
}))


out.table <- cbind(x, outlier)
write.table(x=out.table, file=results$table$file, row.names=F, sep=",")

## WRITE SUMMARY
x.summary <- describe(x$value)
sink(file=results$txt$file)

cat("\n\n")
cat("------------------------------------------\n")
cat("descriptive statistics of the input file:\n")
cat("------------------------------------------\n")

print(x.summary)

cat("\n\n")
cat("------------------------------------------\n")
cat("huber statistics:\n")
cat("------------------------------------------\n")
print(work$huber)
sink()



## PRINT RESULTS
# PRINT result locations for external loadings.
results.json <- noquote(toJSON(results))
cat(file="results.json", results.json)
